package com.example.module_motocircle.ui.post.detail

import android.util.Log
import com.example.module_motocircle.R

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager

import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.Router
import com.example.lib_common.base.BaseActivity

import com.example.lib_common.base.BaseToolBarActivity
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.base.state.UILoader
import com.example.lib_common.base.state.UIState
import com.example.lib_common.observeWithLifecycle
import com.example.module_motocircle.databinding.ActivityDetailsBaseBinding
import com.example.module_motocircle.databinding.LayoutPostDetailsContentBinding
import com.example.module_motocircle.model.CommentsModel
import com.example.module_motocircle.model.PostCommentsModel
import com.example.module_motocircle.ui.post.detail.adapter.PostDetailAdapter
import dagger.hilt.android.AndroidEntryPoint
/**
 * 帖子详情
 */
@AndroidEntryPoint
@Route(path = Router.Ui.PostDetailsActivity)
class PostDetailActivity : BaseActivity<PostDetailViewModel, ActivityDetailsBaseBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.activity_details_base
    val mViewModel: PostDetailViewModel by viewModels()

    val mPostDetailAdapter: PostDetailAdapter by lazy {
        PostDetailAdapter()
    }


    override fun initView() {
        super.initView()
        initUILoader()
        initAdapter()
    }

    private fun initAdapter() {
        mContentBinding.rvPostDetails.adapter = mPostDetailAdapter
        mContentBinding.rvPostDetails.layoutManager = LinearLayoutManager(this)
    }

    private lateinit var mUiLoader: UILoader
    private lateinit var mContentBinding: LayoutPostDetailsContentBinding
    private fun initUILoader() {
        mContentBinding = DataBindingUtil.inflate(
            LayoutInflater.from(this),
            R.layout.layout_post_details_content,
            null,
            false
        )
        mUiLoader = object : UILoader(this) {
            override fun loadSuccessView(container: ViewGroup?): View {
                return mContentBinding.root
            }
        }.apply { updateState(UIState.LOADING) }

        mBinding.layoutContent.addView(mUiLoader)
    }


    override fun initData() {
        mViewModel.getPostDetailData(1,10)
    }

    override fun initListener() {
        mViewModel.mPostState.observeWithLifecycle(this){
            when(it){
                is Loading -> {
                    mUiLoader.updateState(UIState.LOADING)

                }
                is Error ->{
                    mUiLoader.updateState(UIState.NETWORK_ERROR)
                }
                is Success -> {
                    val totalCommentsSize = mViewModel.currentPost?.total ?: 0


                    mUiLoader.updateState(UIState.SUCCESS)
                    //数据
                    mPostDetailAdapter.setPostItem(mViewModel.currentPost!!,totalCommentsSize)
                    mPostDetailAdapter.setAfterSale(mViewModel.afterSale!!)
                    mViewModel.currentCommentsList?.let { it1 -> mPostDetailAdapter.setComments(it1) }
                    setPageData()

                }
                else -> {

                }
            }
        }

        //展开子评论，刷新列表
        mViewModel.showAllCommentsFlow.observeWithLifecycle(this@PostDetailActivity) {
            if (it.isNotEmpty()) {
                mPostDetailAdapter.setCommentsNewData(mutableListOf<PostCommentsModel>().apply { addAll(mViewModel.formatCommentsList) })
            }
        }

        mViewModel.mLoadCommentsPageEvent.observeWithLifecycle(this) {
            mPostDetailAdapter.setCommentsNewData(mutableListOf<PostCommentsModel>().apply { addAll(mViewModel.formatCommentsList) })
        }

    }
    //设置页面数据
    private fun setPageData() {
        val totalCommentsSize = mViewModel.commentSize
        mContentBinding.post = mViewModel.currentPost
        mPostDetailAdapter.setPostInfo(mViewModel.currentPost, totalCommentsSize, mViewModel.get1stPageCommentsNum())
//        mPostDetailAdapter.setAfterSaleInfo(mViewModel.afterSaleInfo)
        mPostDetailAdapter.setCommentsNewData(mutableListOf<PostCommentsModel>().apply { addAll(mViewModel.formatCommentsList) })


        mUiLoader.updateState(UIState.SUCCESS)
    }
    //点击展开评论
    private val onClickShowAll: (CommentsModel, Int) -> Unit by lazy {
        { comments, position ->
            mViewModel.handleShowAllComments(comments, position)
        }
    }

}